//
//  KKStatusOriginalViewCell.swift
//  KKWeibo
//
//  Created by kassia on 16/7/14.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class KKStatusOriginalViewCell: UIView {

    var originalBottom: Constraint?
    //设置原创微博数据
    var statusViewModel: KKStatusViewModel? {
    
        didSet{
        
            
            //给头像赋值
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url{
                
            headImageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage:  UIImage(named: "avatar_default_big"))
            }

            scrennNameLabel.text = statusViewModel?.status?.user?.screen_name // 名字
            
            timeLabel.text = statusViewModel?.timeStr// 时间
            
            mbrankImageView.image = statusViewModel?.mbrandImage //会员等级
            
            sourceLabel.text = statusViewModel?.sourceContent // 来源
            
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage // 认证等级
            
            contentLabel.text = statusViewModel?.status?.text
            
            originalBottom?.uninstall()
            
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
            
                //有配图
                pictureView.hidden = false
                pictureView.picUrls = statusViewModel?.status?.pic_urls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalBottom = make.bottom.equalTo(pictureView).offset(originalMagrinCell).constraint

                })

            } else {
            
                //没有配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalBottom = make.bottom.equalTo(contentLabel).offset(originalMagrinCell).constraint
                })

            }
        }
    }
    /**
        懒加载控件
     */
     //  头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //  认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //  昵称
    private lazy var scrennNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(),fontSize: 15)
    //  会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    //  来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
    //  微博内容
    private lazy var contentLabel: UILabel = {
    
        let lable = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 15)
        
        lable.numberOfLines = 0
        
        return lable
    
    }()
    // 原创微博配图
    private lazy var pictureView: KKStatusPictureView = {
    
        let image = KKStatusPictureView()
        image.backgroundColor = self.backgroundColor
        return image
    
    }()
    
    override init(frame: CGRect) {
    
        super.init(frame: frame)
        
        setupUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(scrennNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        //contentLabel.backgroundColor = UIColor.lightGrayColor()
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(originalMagrinCell)
            make.top.equalTo(self).offset(originalMagrinCell)
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        scrennNameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(originalMagrinCell)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(scrennNameLabel.snp_trailing).offset(originalMagrinCell)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(originalMagrinCell)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(originalMagrinCell)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(originalMagrinCell)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-originalMagrinCell)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(contentLabel).offset(originalMagrinCell)
        }
        
        // 重要约束
        self.snp_makeConstraints { (make) -> Void in
            
           self.originalBottom = make.bottom.equalTo(pictureView).offset(originalMagrinCell).constraint
        }
        
        

    }
    
    
}








